package cg.studio.algorithm.tree;

import org.junit.Test;

import cg.studio.algorithm.tree.TreeTraverse.DeepthTraverseOrder;

public class TreeTester
{
  //@Test
  public void testTraverse()
  {
    TreeTraverse[] traverses = new TreeTraverse[2];
    traverses[0] = new TreeRecursiveTraverse();
    traverses[1] = new TreeStackTraverse();
    
    BinaryTree<Integer> tree = buildTree();
    
    for( DeepthTraverseOrder traverseOrder : DeepthTraverseOrder.values() )
    {
      for( TreeTraverse traverse : traverses )
      {
        traverse.deepthTraverse( tree, traverseOrder );
        System.out.println();
        traverse.deepthTraverse( tree, traverseOrder );
        System.out.println();
      }
    }

  }
  
  @Test
  public void testCommonAncestor()
  {
    BinaryTree<Integer> tree = buildTree();
    
    TreeUtil.getCommonAncestorOptimized( tree, tree.getLeft().getLeft(), tree.getLeft().getRight() ).display();
    TreeUtil.getCommonAncestorOptimized( tree, tree.getLeft().getLeft(), tree.getRight().getLeft() ).display();
  }
  
  protected BinaryTree<Integer> buildTree()
  {
    BinaryTree<Integer> tree = new BinaryTree<Integer>( "1", 1 );
    
    BinaryTree<Integer> tree2 = new BinaryTree<Integer>( "2", 2 );
    tree.setLeft( tree2 );
    
    BinaryTree<Integer> tree3 = new BinaryTree<Integer>( "3", 3 );
    tree.setRight( tree3 );
    
    BinaryTree<Integer> tree4 = new BinaryTree<Integer>( "4", 4 );
    tree2.setLeft( tree4 );

    BinaryTree<Integer> tree5 = new BinaryTree<Integer>( "5", 5 );
    tree2.setRight( tree5 );

    BinaryTree<Integer> tree6 = new BinaryTree<Integer>( "6", 6 );
    tree3.setLeft( tree6 );

    BinaryTree<Integer> tree7 = new BinaryTree<Integer>( "7", 7 );
    tree3.setRight( tree7 );

    BinaryTree<Integer> tree8= new BinaryTree<Integer>( "8", 8 );
    tree5.setLeft( tree8 );
    
    return tree;
  }
}
